package filters;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

@Slf4j
public class CorsFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
        log.info("CorsFilter ___ init ___ 初始化过滤器");
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
        log.info("CorsFilter ___ destroy ___ 销毁过滤器");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        log.info("CorsFilter ___ doFilter ___ 进入了跨域过滤器");

        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpServletRequest request = (HttpServletRequest) servletRequest;

        log.info("CorsFilter ___ doFilter ___ 进入了跨域过滤器 ___ 请求地址是：{}", request.getRequestURL());

        // 配置跨域
        HttpServletResponseHeaderConfig.setHttpServletResponseHeader(response);

        // 拦截一切请求，然后通过这个过滤器
        filterChain.doFilter(request, response);
    }

}
